﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace DBUtility
{
    public class DbHelperAccess
    {
        public OleDbConnection Conn;    
        public string ConnString;//连接字符串   
        
    
    
        #region 构造函数与连接关闭数据库    
    
        public DbHelperAccess(string dbpath)    
        {    
            ConnString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";    
            ConnString += dbpath;
            Conn = new OleDbConnection(ConnString);    
            Conn.Open();    
        }    
    
      
        public OleDbConnection DbConn()    
        {    
            Conn.Open();    
            return Conn;    
        }    
    
      
        public void Close()    
        {    
            Conn.Close();    
        }   
        #endregion    
    
     
        public DataTable SelectToDataTable(string SQL)    
        {    
            OleDbDataAdapter adapter = new OleDbDataAdapter();    
            OleDbCommand command = new OleDbCommand(SQL, Conn);    
            adapter.SelectCommand = command;    
            DataTable Dt = new DataTable();    
            adapter.Fill(Dt);       
            return Dt;    
        }    
   
       // public DataSet SelectToDataSet(string SQL, string subtableName)    
         public DataSet SelectToDataSet(string SQL)    
        {    
            OleDbDataAdapter adapter = new OleDbDataAdapter();    
            OleDbCommand command = new OleDbCommand(SQL, Conn);    
            adapter.SelectCommand = command;    
            DataSet Ds = new DataSet();    
            //Ds.Tables.Add(subtableName);    
            //adapter.Fill(Ds, subtableName);                
            adapter.Fill(Ds);  
            return Ds;    
        }    
    
        
        public DataSet SelectToDataSet (string SQL, string subtableName, DataSet DataSetName)    
        {    
            OleDbDataAdapter adapter = new OleDbDataAdapter();    
            OleDbCommand command = new OleDbCommand(SQL, Conn);    
            adapter.SelectCommand = command;    
            DataTable Dt = new DataTable();    
            DataSet Ds = new DataSet();    
            Ds = DataSetName;    
            adapter.Fill(DataSetName, subtableName);    
            return Ds;    
        }    
    
       
        public OleDbDataAdapter SelectToOleDbDataAdapter(string SQL)    
        {    
            OleDbDataAdapter adapter = new OleDbDataAdapter();    
            OleDbCommand command = new OleDbCommand(SQL, Conn);    
            adapter.SelectCommand = command;    
            return adapter;    
        }    
     
        public bool ExecuteSQLNonquery(string SQL)    
        {    
            OleDbCommand cmd = new OleDbCommand(SQL, Conn);    
            try  
            {    
                cmd.ExecuteNonQuery();    
                return true;    
            }    
            catch(Exception ex )
            {    
                return false;    
            }    
        }   

    }
}
